<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PartyEm;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class PartyEmController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PartyEm(), function (Grid $grid) {
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                $grid->model()->where('region_id', $region_id);
                $grid->model()->orderBy('id', 'desc');
            }

            $grid->column('id')->sortable();
            $grid->column('ems_type')->display(function ($ems_type) {
                // 信件类别;意见建议1、申诉2、控告3、其他4
                switch ($ems_type) {
                    case 1:
                        return '意见建议';
                        break;
                    case 2:
                        return '申诉';
                        break;
                    case 3:
                        return '控告';
                        break;
                    default:
                        return '其他';
                }
            });
            $grid->column('ems_title');
            $grid->column('ems_open')->display(function ($ems_open) {
                return $ems_open ? '是' : '否';
            });
            $grid->column('ems_writer');
            $grid->column('ems_sex');
            $grid->column('ems_address');
            $grid->column('ems_mobile');
            $grid->column('status')->select([0 => '待处理', 1 => '处理中', 2 => '已处理']);
            $grid->column('created_at', '提交时间')->sortable();

            $grid->disableEditButton(); // 禁用编辑按钮
            $grid->disableCreateButton(); // 禁用新增按钮
            $grid->quickSearch(['ems_writer', 'ems_mobile', 'ems_address', 'ems_content'])->placeholder('搜索...');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('status', '状态筛选', [0 => '待处理', 1 => '处理中', 2 => '已处理']);
                $selector->selectOne('ems_type', '类型筛选', [1 => '意见建议', 2 => '申诉', 3 => '控告', 4 => '其他']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PartyEm(), function (Show $show) {
            $show->field('id');
            $show->field('ems_title');
            $show->field('ems_type')->as(function ($ems_type) {
                switch ($ems_type) {
                    case 1:
                        return '意见建议';
                        break;
                    case 2:
                        return '申诉';
                        break;
                    case 3:
                        return '控告';
                        break;
                    default:
                        return '其他';
                }
            });

            $show->field('ems_content', '内容')->value();
            $show->field('ems_open')->as(function ($ems_open) {
                return $ems_open ? '是' : '否';
            });
            $show->field('ems_writer');
            $show->field('ems_sex')->as(function ($ems_sex) {
                return $ems_sex ? '男' : '女';
            });
            $show->field('ems_address');
            $show->field('ems_mobile');
            $show->field('status')->as(function ($ems_type) {
                switch ($ems_type) {
                    case 0:
                        return '待处理';
                        break;
                    case 1:
                        return '处理中';
                        break;
                    default:
                        return '已处理';
                }
            });
            $show->field('region_id')->as(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '';
                }
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PartyEm(), function (Form $form) {
            $form->display('id');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
